/*
 * JStock - Free Stock Market Software
 * Copyright (C) 2009 Yan Cheng Cheok <yccheok@yahoo.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
 * PortfolioSelectionJPanel.java
 *
 * Created on Sep 19, 2009, 1:24:36 PM
 */

package org.yccheok.jstock.gui.file;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JRadioButton;
import org.yccheok.jstock.gui.Utils;
import org.yccheok.jstock.internationalization.GUIBundle;

/**
 *
 * @author yccheok
 */
public class PortfolioSelectionJPanel extends javax.swing.JPanel {

    /** Creates new form PortfolioSelectionJPanel */
    public PortfolioSelectionJPanel() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        jRadioButton4 = new javax.swing.JRadioButton();

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/yccheok/jstock/data/gui"); // NOI18N
        setBorder(javax.swing.BorderFactory.createTitledBorder(bundle.getString("PortfolioSelectionJPanel_Title"))); // NOI18N

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setSelected(true);
        jRadioButton1.setText(bundle.getString("PortfolioSelectionJPanel_SaveBuy")); // NOI18N

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText(bundle.getString("PortfolioSelectionJPanel_SaveSell")); // NOI18N

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setText(bundle.getString("PortfolioSelectionJPanel_SaveCash")); // NOI18N

        buttonGroup1.add(jRadioButton4);
        jRadioButton4.setText(bundle.getString("PortfolioSelectionJPanel_SaveDividend")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2)
                    .addComponent(jRadioButton3)
                    .addComponent(jRadioButton4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jRadioButton4)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        Component[] components = getComponents();
        if (components != null && components.length > 0) {
        int count = components.length;
        for (int i = 0; i < count; i++)
            components[i].setEnabled(enabled);
        }
    }
    
    public String getSuggestedFileName() {
        final JRadioButton button = Utils.getSelection(buttonGroup1);
        /* Impossible. Just to be paranoid. */
        if (button == null) {
            return GUIBundle.getString("PortfolioSelectionJPanel_SaveBuySuggestedFileName");
        }
        if (button.getText().equals(GUIBundle.getString("PortfolioSelectionJPanel_SaveBuy"))) {
            return GUIBundle.getString("PortfolioSelectionJPanel_SaveBuySuggestedFileName");
        }
        else if (button.getText().equals(GUIBundle.getString("PortfolioSelectionJPanel_SaveSell"))) {
            return GUIBundle.getString("PortfolioSelectionJPanel_SaveSellSuggestedFileName");
        }
        else if (button.getText().equals(GUIBundle.getString("PortfolioSelectionJPanel_SaveCash"))) {
            return GUIBundle.getString("PortfolioSelectionJPanel_SaveCashSuggestedFileName");
        }
        else if (button.getText().equals(GUIBundle.getString("PortfolioSelectionJPanel_SaveDividend"))) {
            return GUIBundle.getString("PortfolioSelectionJPanel_SaveDividendSuggestedFileName");
        }
        return GUIBundle.getString("PortfolioSelectionJPanel_SaveBuySuggestedFileName");
    }

    public org.yccheok.jstock.file.Statement.Type getType() {
        final JRadioButton button = Utils.getSelection(buttonGroup1);
        /* Impossible. Just to be paranoid. */
        if (button == null) {
            return org.yccheok.jstock.file.Statement.Type.PortfolioManagementBuy;
        }
        if (button.getText().equals(GUIBundle.getString("PortfolioSelectionJPanel_SaveBuy"))) {
            return org.yccheok.jstock.file.Statement.Type.PortfolioManagementBuy;
        }
        else if (button.getText().equals(GUIBundle.getString("PortfolioSelectionJPanel_SaveSell"))) {
            return org.yccheok.jstock.file.Statement.Type.PortfolioManagementSell;
        }
        else if (button.getText().equals(GUIBundle.getString("PortfolioSelectionJPanel_SaveCash"))) {
            return org.yccheok.jstock.file.Statement.Type.PortfolioManagementDeposit;
        }
        else if (button.getText().equals(GUIBundle.getString("PortfolioSelectionJPanel_SaveDividend"))) {
            return org.yccheok.jstock.file.Statement.Type.PortfolioManagementDividend;
        }
        return org.yccheok.jstock.file.Statement.Type.PortfolioManagementBuy;        
    }

    public void addJRadioButtonsActionListener(ActionListener actionListener) {
        jRadioButton1.addActionListener(actionListener);
        jRadioButton2.addActionListener(actionListener);
        jRadioButton3.addActionListener(actionListener);
        jRadioButton4.addActionListener(actionListener);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton jRadioButton4;
    // End of variables declaration//GEN-END:variables

}
